<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\IntegralCommodity;
use App\OperationRecord;
use App\PrizeRecords;
use App\SignConfig;
use App\User;
use Illuminate\Http\Request;
use DB;

/**
 * Class OperationRecordController
 * @package App\Http\Controllers
 */
class OperationRecordController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 提现列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function operationRecordList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::operationRecordList());
        }

        return view('operationRecord.operationRecordList');
    }

    /**
     * 提现不通过
     * @param OperationRecord $operationRecord
     * @return array
     */
    public function operationRecordNo(Request $request, $operationRecord_id)
    {
        return OperationRecord::WithdrawOff($operationRecord_id, $request->check_content);
    }

    /**
     * 提现通过
     * @param OperationRecord $operationRecord
     * @return array
     */
    public function operationRecordYes(OperationRecord $operationRecord)
    {
        //提现到支付宝
        if ($operationRecord->reflect_type === OperationRecord::REFLECT_TYPE_ALIPAY) {
            return OperationRecord::WithdrawAlipay($operationRecord);
        } else if ($operationRecord->reflect_type === OperationRecord::REFLECT_TYPE_WECHAT) {
            //提现到微信
            return OperationRecord::WithdrawWechat($operationRecord);
        } else {
            //话费充值
            return OperationRecord::PhoneRecharge($operationRecord);
        }
    }


    /**
     * 用户轨迹
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function userOperationList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::UserOperationList());
        }

        return view('operationRecord.userOperationList');
    }


    /**
     * 实体商品
     * @param OperationRecord $operationRecord
     * @return $this
     */
    public function operationRealDetail(OperationRecord $operationRecord)
    {
        $address = $operationRecord->sheng . $operationRecord->shi . $operationRecord->xian . $operationRecord->detail;
        return response()->json([
            'name' => $operationRecord->user_name,
            'phone' => $operationRecord->phone,
            'address' => $address,
        ]);
    }

    /**
     * 发货
     * @param Request $request
     * @param OperationRecord $operationRecord
     * @return array
     */
    public function operationRealDeliver(Request $request, OperationRecord $operationRecord)
    {
        if (!$request->has('express_name') || !$request->has('express_order')) {
            return ['status' => 2, 'message' => '请填写快递信息'];
        }
        $operationRecord->express_name = $request->express_name;
        $operationRecord->express_order = $request->express_order;
        $operationRecord->status = OperationRecord::STATUS_YES;
        if (!$operationRecord->save()) {
            return ['status' => 2, 'message' => '发货失败'];
        }

        return ['status' => 1, 'message' => '发货成功'];
    }

    /**
     * 积分抽奖列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function operationRecordLuckdrawList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::OperationRecordLuckdrawList());
        }

        return view('operationRecord.operationRecordLuckdrawList');
    }

    /**
     * 拒绝
     * @param PrizeRecords $prizeRecords
     * @param Request $request
     * @return array
     */
    public function operationRecordLuckdrawRefuse(PrizeRecords $prizeRecords, Request $request)
    {
        return PrizeRecords::Refuse($prizeRecords, $request);
    }


    /**
     * 通过
     * @param PrizeRecords $prizeRecords
     * @param Request $request
     * @return array
     */
    public function operationRecordLuckdrawAgree(PrizeRecords $prizeRecords, Request $request)
    {
        return PrizeRecords::Agree($prizeRecords, $request);
    }

    /**
     * @param PrizeRecords $prizeRecords
     * @return array
     */
    public function luckdrawInfo(PrizeRecords $prizeRecords)
    {
        $address = $prizeRecords->sheng . $prizeRecords->shi . $prizeRecords->xian . $prizeRecords->detail;
        return [
            'name' => $prizeRecords->user_name,
            'phone' => $prizeRecords->phone,
            'address' => $address,
        ];
    }


    /**
     * 批量通过支付宝和充话费
     * @param Request $request
     * @return array
     */
    public function aliAll(Request $request)
    {
        $modify = OperationRecord::whereIn('id', array_filter($request->ids))
            ->update([
                'status' => OperationRecord::STATUS_YES,
            ]);
        if (!$modify) {
            return ['status' => 2, 'message' => '提交失败'];
        }
        return ['status' => 1, 'message' => '提交成功'];
    }


    /**
     * 微信批量提现
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Exception
     */
    public function wechatAll(Request $request)
    {
        return response()->json(OperationRecord::WechatAll(array_filter($request->ids)));
    }


    /**
     * 操作积分管理
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function tasks(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::tasks());
        }

        return view('operationRecord.tasks');
    }

    /**
     * 操作积分管理
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function tasksInfo($id)
    {
        $info = DB::table('day_tasks')->select('name', 'number', 'frequency', 'content', 'add_number')->where('id', $id)->first();
        return response()->json([
            'name' => $info->name,
            'number' => $info->number,
            'frequency' => $info->frequency,
            'content' => $info->content,
            'add_number' => $info->add_number,
        ]);
    }

    /**
     * 操作积分管理修改
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function tasksEdit($id, Request $request)
    {
        return response()->json(OperationRecord::TasksEdit($id, $request));
    }
	
	//统计总金额
	public function tongjiList(Request $request){
        if ($request->isMethod('post')) {
            return response()->json(Datatables::tixiantongji());
        }

        return view('operationRecord.tixiantongji');		
	}
	
	//签到设置
	public function set(Request $request){
		$config = SignConfig::find(1);
        if ($request->isMethod('post')) {
            $return = SignConfig::ConfigModify($config, $request);

            return redirect('/operationRecord/set')->with('status', $return['status'])->with('message', $return['message']);
            
        }

        return view('operationRecord.signcofig')->with(compact('config'));
	}
}
